import React from 'react';
import {Modal, Form, Input, Button, message, Divider} from 'antd';
import axios from 'axios';
import {injectIntl} from 'react-intl';


const FormItem = Form.Item;

@Form.create()
class ModifyPass extends React.Component {
    constructor(props) {
        super(props);
        this.userId = this.props.match.params.userId;
        this.handleSubmit = this.handleSubmit.bind(this);
        this.checkPassword = this.checkPassword.bind(this);
        this.checkPasRule = this.checkPasRule.bind(this);
    }

    formItemLayout = {
        labelCol: {span: 1, offset: 0},
        wrapperCol: {span: 5},
    };


    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                axios.post('/api/corp/psw/mod', {
                    oldPwd: values.oldPwd,
                    newPwd: values.newPwd
                }).then(res => {
                    if (res && res.code == 200) {
                        message.success('修改成功');
                    }
                    this.props.form.resetFields();
                });
            }
            return null;
        })
    }

    /**
     *  验证两次输入的密码
     * @param rule
     * @param value
     * @param callback
     */
    checkPassword(rule, value, callback) {
        const form = this.props.form;
        const common = this.props.intl.messages.common;

        if (value && value !== form.getFieldValue('newPwd')) {
            callback(common['2passwordInconsistent']);
        } else {
            callback();
        }
    }


    /**
     *  验证两次输入的密码
     * @param rule
     * @param value
     * @param callback
     */
    checkPasRule(rule, value, callback) {
        const form = this.props.form;
        const common = this.props.intl.messages.common;
        let patrn = /^[0-9a-zA-Z]{6,16}$/;
        if (value && !patrn.test(value)) {
            callback(common.cannot6less);
        } else {
            callback();
        }
    }


    render() {
        const {getFieldDecorator} = this.props.form;
        const common = this.props.intl.messages.common;

        return (
            <div>
                <Form onSubmit={this.handleSubmit}>
                    {/*<FormItem>*/}
                    {/*{getFieldDecorator('id')(<Input type="hidden" />)}*/}
                    {/*</FormItem>*/}
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('oldPwd', {
                            rules: [
                                {
                                    required: true,
                                    message: common.pleaseEnterOldPass
                                }
                            ]
                        })(<Input placeholder={common.oldPass} type="password"/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('newPwd', {
                            rules: [
                                {
                                    required: true,
                                    message: common.pleaseEnterNewPass
                                }, {
                                    validator: this.checkPasRule
                                }
                            ]
                        })(<Input placeholder={common.newPass} type="password"/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('newPwdTwo', {
                            rules: [
                                {
                                    required: true,
                                    message: common.pleaseEnterNewPassAgain
                                }, {
                                    validator: this.checkPassword
                                }
                            ]
                        })(<Input placeholder={common.pleaseEnterNewPassAgain} type="password"/>)}
                    </FormItem>
                    <FormItem wrapperCol={{span: 16, offset: 0}} style={{marginTop: 24}}>
                        <Button type="primary" htmlType="submit">{common.sure}</Button>
                    </FormItem>
                </Form>
            </div>
        );
    }
}

export default injectIntl(ModifyPass);